<template>
    
    <div class="register">
      <el-form ref="registerForm" :model="registerForm" :rules="registerRules" label-width="100px"  class="form">
        <h1 class="title">管理员注册</h1>
        <el-form-item label="账号" prop="name"  >
          <el-input v-model="registerForm.name"></el-input>
        </el-form-item>
        <el-form-item label="密码" prop="password">
          <el-input type="password" v-model="registerForm.password"></el-input>
        </el-form-item>
        <el-form-item label="确认密码" prop="confirmPassword">
          <el-input type="password" v-model="registerForm.confirmPassword"></el-input>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="submitForm('registerForm')">注册</el-button>
          <el-button @click="resetForm('registerForm')">重置</el-button>
        </el-form-item>
      </el-form>
    </div>
  </template>
  
  <script>
  export default {
    data() {

      // 验证两次密码输入是否一致
      var validatePass1 = (rule, value, callback) => {
    if (value === '') {
        callback(new Error('请输入密码'));
    } else {
     if (this.registerForm.confirmPassword !== '') {
       this.$refs.registerForm.validateField('confirmPassword');
     }
     callback();
    }
   };
   var validatePass2 = (rule, value, callback) => {
     if (value === '') {
       callback(new Error('请再次输入密码'));
     } else if (value !== this.registerForm.password) {
       callback(new Error('两次输入密码不一致!'));
     } else {
       callback();
     }
   };

      return {
        registerForm: {
          name: '',
          password: '',
          confirmPassword: '',
        },
        registerRules: {
          name: [
            { required: true, message: '请输入用户名', trigger: 'blur' },
            { min: 3, max: 20, message: '用户名长度在 3 到 20 个字符', trigger: 'blur' }
          ],
          password: [
            { required: true, message: '请输入密码', trigger: 'blur' },
            { min: 6, max: 20, message: '密码长度在 6 到 20 个字符', trigger: 'blur' },
            { validator: validatePass1, trigger: 'blur'}
          ],
          confirmPassword: [
            { required: true, message: '请再次输入密码', trigger: 'blur' },
            { validator: validatePass2, trigger: 'blur', required: true }
          ],
        }
    }
},
methods: {
        submitForm() {
            this.$refs.registerForm.validate(valid => {
                if (valid) {
                  this.request.post("/admin/register",
                  {
                        name: this.registerForm.name,
                        password: this.registerForm.password,
                 }).then((response) => { 
                            if (response.data.code === 200 || response.data.code === 400) {
                              this.$message.success('注册成功!')
                                this.$router.push('/login')
                            } else {
                                this.$message.error('账号已存在，请重新输入！')
                            }
                        })
                        .catch(error => {
                            console.log(error)
                        })
                }
            })
        },
        resetForm(){
          this.$refs.registerForm.resetFields()
        }
    }
}
  </script>

<style>
.register{
            box-sizing: border-box;
            width: 100%;
            height: 100%;
            padding-top: 5%;
            background-image: url("../../assets/Manager.jpg");
            padding: 15%;
}
.form{
            border-radius: 10px;
            margin: 0px auto;
            width: 300px;
            padding: 30px 35px 15px 35px;
            background: #fff;
            border: 1px solid #eaeaea;
            text-align: left;
            box-shadow: 0 0 20px 2px rgba(0, 0, 0, 0.1);
}

.title {
            margin: 0px auto 40px auto;
            text-align: center;
            color: #505458;
        }
</style>